package org.facyt.vacas.dao.impl;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.facyt.vacas.dao.IGenericDAO;

public class GenericDAO<T, S> implements IGenericDAO<T, S> {

	@PersistenceContext
	private EntityManager entityManager;

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public T findById(Class tClass, S id) {
		return (T) this.entityManager.find(tClass, id);
	}

	public Collection<T> findAll(Class clazz) {
		Collection<T> result =  this.entityManager.createQuery("select n from " + clazz.getName() + " n").getResultList();
		
		return result;
		
	}

	public void remove(T entity) {
		this.entityManager.remove(entity);
	}

	public T persit(T entity) {
		this.entityManager.persist(entity);
		return entity;
	}

	public T update(T entity) {
		this.entityManager.merge(entity);
		return entity;
	}

}
